package com.dd.cloud.user.service.product.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.UserConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.doctor.DoctorEquity;
import com.dd.cloud.user.entity.doctor.DoctorPresenterNum;
import com.dd.cloud.user.entity.doctor.DoctorUser;
import com.dd.cloud.user.entity.product.ProductGiveRecord;
import com.dd.cloud.user.mapper.product.ProductGiveRecordMapper;
import com.dd.cloud.user.req.product.ProductGiveRecordSystemPageReq;
import com.dd.cloud.user.res.product.ProductGiveRecordRes;
import com.dd.cloud.user.res.product.ProductGiveRecordSystemPageRes;
import com.dd.cloud.user.service.doctor.IDoctorDetailService;
import com.dd.cloud.user.service.doctor.IDoctorEquityService;
import com.dd.cloud.user.service.doctor.IDoctorUserService;
import com.dd.cloud.user.service.product.IProductGiveRecordService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Lx
 * @version 1.0
 * @date 2024/1/23
 */
@Slf4j
@Service
public class ProductGiveRecordServiceImpl extends ServiceImpl<ProductGiveRecordMapper, ProductGiveRecord> implements IProductGiveRecordService {

    @Autowired
    private IDoctorUserService doctorUserService;

    @Autowired
    private IDoctorEquityService doctorEquityService;

    /**
     * 查询收费服务产品赠送记录
     *
     * @param req
     * @return
     */
    @Override
    public IPage<ProductGiveRecordSystemPageRes> queryProductGiveRecordSystemPage(ProductGiveRecordSystemPageReq req) {
        IPage<ProductGiveRecordSystemPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryProductGiveRecordSystemPage(iPage, req);
        return iPage;
    }

    @Override
    public ProductGiveRecordRes remindDoctorTip(Integer isReceive) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Integer doctorId = loginInfo.getId();
        LambdaQueryWrapper<ProductGiveRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductGiveRecord::getDoctorId, doctorId)
                .in(ProductGiveRecord::getIsAuth, 0, 1)
                .eq(ProductGiveRecord::getReceive, isReceive)
                .eq(ProductGiveRecord::getStatus, 1);
        if (isReceive == 1) {
            queryWrapper.orderByAsc(ProductGiveRecord::getGiveTime);
        }
        List<ProductGiveRecord> list = this.list(queryWrapper);
        ProductGiveRecordRes productGiveRecordRes = new ProductGiveRecordRes();
        productGiveRecordRes.setIsShow(0);
        if (CollectionUtil.isEmpty(list)) {
            return productGiveRecordRes;
        }
        ProductGiveRecord productGiveRecord = list.get(0);
        productGiveRecordRes.setNum(list.size());
        productGiveRecordRes.setIsShow(1);
        productGiveRecordRes.setIsAuth(productGiveRecord.getIsAuth());
        productGiveRecordRes.setIsCreateOrder(productGiveRecord.getIsCreateOrder());
        productGiveRecordRes.setProductGiveId(productGiveRecord.getId());
        return productGiveRecordRes;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePresenterUpdateStatus(Integer giveId, Integer status) {
        ProductGiveRecord productGiveRecord = this.getById(giveId);
        if (productGiveRecord == null) {
            throw new ZekeException("记录不存在");
        }
        if (status == 2) {
            productGiveRecord.setReceive(2);
            productGiveRecord.setGiveTime(LocalDateTime.now());
        } else if (status == 3) {
            productGiveRecord.setStatus(3);
            productGiveRecord.setUseDate(LocalDateTime.now());
        }
        boolean flag = this.updateById(productGiveRecord);
        if (!flag) {
            throw new ZekeException("更新状态失败");
        }
    }

    @Override
    public Boolean queryIsMember() {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Integer doctorId = loginInfo.getId();
        LambdaQueryWrapper<DoctorEquity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorEquity::getDoctorId, doctorId)
                .gt(DoctorEquity::getExpirDate, LocalDateTime.now());
        Integer count = doctorEquityService.count(queryWrapper);
        if (count > 0) {
            return true;
        }
        return false;
    }

}
